package edu.hcmus.sow.web;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.validation.Errors;

import edu.hcmus.sow.dao.SiteDAO;
import edu.hcmus.sow.domain.Site;

public class SiteForm {
   private Integer id;
   @NotEmpty(message = "Sub Domain must not be blank.")
   private String subDomain;
   private String numberOfUsers;
   private String storageLimit;
   private String defaultLanguage;
   private String shortDateFormat;
   private String renewDate;

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public String getSubDomain() {
      return StringUtils.isEmpty(subDomain) ? subDomain : subDomain.trim().replace(' ', '_').toLowerCase();
   }

   public void setSubDomain(String subDomain) {
      this.subDomain = subDomain;
   }

   public String getNumberOfUsers() {
      return numberOfUsers;
   }

   public Integer getNumberOfUsersInt() {
      return StringUtils.isEmpty(numberOfUsers) ? null : new Integer(numberOfUsers);
   }

   public void setNumberOfUsers(String numberOfUsers) {
      this.numberOfUsers = numberOfUsers;
   }

   public String getStorageLimit() {
      return storageLimit;
   }

   public Integer getStorageLimitInt() {
      return StringUtils.isEmpty(storageLimit) ? null : new Integer(storageLimit);
   }

   public void setStorageLimit(String storageLimit) {
      this.storageLimit = storageLimit;
   }

   public String getDefaultLanguage() {
      return defaultLanguage;
   }

   public void setDefaultLanguage(String defaultLanguage) {
      this.defaultLanguage = defaultLanguage;
   }

   public String getShortDateFormat() {
      return shortDateFormat;
   }

   public void setShortDateFormat(String shortDateFormat) {
      this.shortDateFormat = shortDateFormat;
   }

   public String getRenewDate() {
      return renewDate;
   }

   public Date getRenewDateDt() {
      try {
         return new SimpleDateFormat("MM/dd/yyyy").parse(renewDate);
      } catch (Exception ex) {
         return null;
      }
   }

   public void setRenewDate(String renewDate) {
      this.renewDate = renewDate;
   }

   public void validate(Errors errors, SiteDAO siteDAO) {
      if (StringUtils.isNotEmpty(numberOfUsers)) {
         if (!NumberUtils.isDigits(numberOfUsers) || new Integer(numberOfUsers).intValue() <= 0) {
            errors.rejectValue("numberOfUsers", "err.numberOfUsers", "Number of users must be a positive integer.");
         }
      }

      if (StringUtils.isNotEmpty(storageLimit)) {
         if (!NumberUtils.isDigits(storageLimit) || new Integer(storageLimit).intValue() <= 0) {
            errors.rejectValue("storageLimit", "err.storageLimit", "Storage limit must be a positive integer.");
         }
      }

      if (StringUtils.isNotEmpty(renewDate)) {
         try {
            new SimpleDateFormat("MM/dd/yyyy").parse(renewDate);
         } catch (Exception ex) {
            errors.rejectValue("renewDate", "err.renewDate", "Renew date must be in format MM/dd/yyyy.");
         }
      }

      if (!errors.hasFieldErrors("subDomain")) {
         Site site = siteDAO.findBySubDomain(getSubDomain());
         if (site != null && site.getId().intValue() != id.intValue()) {
            errors.rejectValue("subDomain", "err.subDomain", "Sub Domain already exists.");
         }
      }
   }
}
